package com.cskaoyan.shiro;

import com.cskaoyan.bean.domain.*;
import com.cskaoyan.mapper.PermissionMapper;
import com.cskaoyan.mapper.UserMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.System;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongProject: mall35th
 * @BelongPackage: com.cskaoyan.shiro
 * @Author: zijin
 * @Createtime: 2021-12-02 星期四 11:35
 * @Description:
 * authentication 认证
 * authorization 授权
 * 微信real之后再写、】
 */
@Component
public class WxRealm extends AuthorizingRealm {
    @Autowired
    UserMapper userMapper;

    @Autowired
    PermissionMapper permissionMapper;

    /**
     * 获得认证信息
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        /**
         * 提供认证信息
         * authenticationToken参数来源于subject.login时候携带的参数
         */
        MallToken token = (MallToken) authenticationToken;
        String type = token.getType();
        if (!"wx".equals(type)){
            return null;
        }
        String username = token.getUsername();
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        if (users == null || users.size() == 0){
            return null;
        }
        User user = users.get(0);
        System.out.println(user.getPassword());
        String credential = user.getPassword();
        /**
         *第一个参数：principal应用程序中维护的principal信息，如果认证成功，就会维护在session中
         * 第二个参数：credentials 需要的是正确的凭据
         * 第三个参数：realm的名称
         */
        return new SimpleAuthenticationInfo(user,credential,this.getName());
    }

    /**
     * 这个暂时没有做
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        Admin primaryPrincipal = (Admin) principalCollection.getPrimaryPrincipal();
//        Integer[] roleIds = primaryPrincipal.getRoleIds();
//        //根据用户信息获取所有的权限信息
//        List<String> permission = queryFromDb(roleIds);
//        //给授权信息，增加上该用户所拥有的权限
//        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//        simpleAuthorizationInfo.addStringPermissions(permission);
//        return simpleAuthorizationInfo;
        return null;
    }

    /**
     * 根据角色数组，查询所有的权限
     * @param ids
     * @return
     */
    private List<String> queryFromDb(Integer[] ids) {
        ArrayList<String> strings = new ArrayList<>();
        for (Integer id : ids) {
            PermissionExample permissionExample = new PermissionExample();
            permissionExample.createCriteria().andRoleIdEqualTo(id);
            List<Permission> permissions = permissionMapper.selectByExample(permissionExample);
            for (Permission permission : permissions) {
                strings.add(permission.getPermission());
            }
        }
        return strings;
    }
}
